<?php
/**
 * ================================
 * =========发送短信类（云片）=========
 * ================================
 * $Author: jackzheng $
 * 2016/10/20
*/

class ypsms 
{

	public $error = array();
	public function send($phone)
	{
		$rand  = rand(100000,999999);
		
		$apikey = "27b4a6016dc365d72acdf74e0aba553d"; //修改为您的apikey(https://www.yunpian.com)登陆官网后获取
		$mobile = $phone; //请用自己的手机号代替
		
		$ch = curl_init();

		/* 设置验证方式 */

		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

		/* 设置返回结果为流 */
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		/* 设置超时时间*/
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);

		/* 设置通信方式 */
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// 取得用户信息
		$json_data = $this->get_user($ch,$apikey);
		$array = json_decode($json_data,true);
		
		// 发送模板短信
		// 需要对value进行编码
		$data=array('tpl_id'=>'1960968','tpl_value'=>urlencode('#code#').'='.urlencode($rand),'apikey'=>$apikey,'mobile'=>$mobile);
		
		$json_data = $this->tpl_send($ch,$data);
		
		$res_msg = array();
		if($array['code'] == 0)
		{		  
			
			$res_msg['code'] = '100';
			$res_msg['msg'] = '已发送，请注意查收';
			$res_msg['data'] = $rand;
		}	
		else 
		{
			$msg = 'phone:'.$phone.' msg:'.$array['msg'];
			$this->logger($msg);
			
			$res_msg['code'] = '1011';
			$res_msg['msg'] = '信息发送失败';
			$res_msg['data'] = $rand;
		}
		
		return $res_msg;
		
	}

	public function send_msg($phone,$name)
	{
		$rand  = rand(100000,999999);
		
		$apikey = "27b4a6016dc365d72acdf74e0aba553d"; //修改为您的apikey(https://www.yunpian.com)登陆官网后获取
		$mobile = $phone; //请用自己的手机号代替
		
		$ch = curl_init();

		/* 设置验证方式 */

		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

		/* 设置返回结果为流 */
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		/* 设置超时时间*/
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);

		/* 设置通信方式 */
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// 取得用户信息
		$json_data = $this->get_user($ch,$apikey);
		$array = json_decode($json_data,true);
		
		// 发送模板短信
		// 需要对value进行编码
		$data=array('tpl_id'=>'2178634','tpl_value'=>urlencode('#name#').'='.urlencode($name),'apikey'=>$apikey,'mobile'=>$mobile);
		
		$json_data = $this->tpl_send($ch,$data);
		
		$res_msg = array();
		if($array['code'] == 0)
		{		  
			
			$res_msg['code'] = '100';
			$res_msg['msg'] = '已发送，请注意查收';
			$res_msg['data'] = $rand;
		}	
		else 
		{
			$msg = 'phone:'.$phone.' msg:'.$array['msg'];
			$this->logger($msg);
			
			$res_msg['code'] = '1011';
			$res_msg['msg'] = '信息发送失败';
			$res_msg['data'] = $rand;
		}
		
		return $res_msg;
		
	}
	
	
	public function send_inform($info=array(),$tpl_id='')
	{
		
		$apikey = "27b4a6016dc365d72acdf74e0aba553d"; //修改为您的apikey(https://www.yunpian.com)登陆官网后获取
		$mobile = $info['mobile']; //请用自己的手机号代替
		
		$ch = curl_init();

		/* 设置验证方式 */

		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8', 'Content-Type:application/x-www-form-urlencoded','charset=utf-8'));

		/* 设置返回结果为流 */
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

		/* 设置超时时间*/
		curl_setopt($ch, CURLOPT_TIMEOUT, 10);

		/* 设置通信方式 */
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		// 取得用户信息
		$json_data = $this->get_user($ch,$apikey);
		$array = json_decode($json_data,true);
		
		// 发送模板短信
		// 需要对value进行编码
		if($tpl_id =='1741332'){
			$data=array('tpl_id'=>'1741332','tpl_value'=>urlencode('#name#').'='.urlencode($info['name']),'apikey'=>$apikey,'mobile'=>$mobile);

		}
		
		$json_data = $this->tpl_send($ch,$data);
		
		$res_msg = array();
		if($array['code'] == 0)
		{		  
			
			$res_msg['code'] = '100';
			$res_msg['msg'] = '已发送，请注意查收';
			$res_msg['data'] = $info['name'];
		}	
		else 
		{	
			$res_msg['code'] = '1011';
			$res_msg['msg'] = '信息发送失败';
			$res_msg['data'] = $info['name'];
		}
		
		return $res_msg;
		
	}
	

	/***************************************************************************************/
	//获得账户
	private function get_user($ch,$apikey){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/user/get.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('apikey' => $apikey)));
    return curl_exec($ch);
	}
	private function send_sms($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
	}
	
	private function tpl_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'https://sms.yunpian.com/v1/sms/tpl_send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
	}
	
	private function voice_send($ch,$data){
    curl_setopt ($ch, CURLOPT_URL, 'http://voice.yunpian.com/v1/voice/send.json');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    return curl_exec($ch);
	}
	
	//日志记录
	private function logger($log_content)
	{
		$max_size = 100000;
		$log_filename=$_SERVER['DOCUMENT_ROOT'].'/Mapi/log/sms_'.date('Y-m-d').'.txt';
		if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
		file_put_contents($log_filename, date('Y-m-d H:i:s')." ".$log_content."\r\n", FILE_APPEND);
	}
	

}
?>